package com.hnkywl.sts.action.zjgl;

import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Xyz;
import com.hnkywl.sts.service.organ.KhhService;
import com.hnkywl.sts.service.organ.NbgsService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.zjgl.PjglService;
import com.hnkywl.sts.service.zjgl.XyzService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sfgl
 * 类描述:    岗位Action类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/zjgl/xyz")
public class XyzAction extends SimpleActionSupport {
    private XyzService xyzService;
    //信用证实例
    private Xyz xyz;
    //信用证Id
    private Long xyzId;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    //信用证收付类型 应收信用 应付信用证
    private Integer xyzsflxType = 0;
    //审核状态  未审核 已审核
    private Integer shztType;
    //公司ID
    private Long gsId;
    //公司名称
    private String gsMc;
    //开户行Id
    private Long khhId;
    //开户行名称
    private String khhMc;
   //批量上传的pjId
    private String xyzIds;
    private  List<Xyz> xyzList = new ArrayList<Xyz>();
    //申请人Id
    private Long sqrId;

    private String sysDate;


    private KhhService khhService;
    private Long sfkId;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private Long yhmcId;

    private String xyzhm;

    private Integer zt;



    //票据查询分页显示
    @Action("xyzList")
    public void xyzList() {
        if(zt==null){
            zt =1;
        }
        this.renderJson(this.xyzService.findAllXyz(xyzhm,sfkId, xyzsflxType, shztType, gsId, gsMc, khhId, khhMc, zt,page, rows));
    }

    @Action("xyzListByIds")
    public void xyzListByIds() {
        this.renderJson(this.xyzService.findxyzListByIds(xyzIds, xyzsflxType, page, rows));
    }


    @Action("xyz_content")
    public String xyzContent() {
        //进入update页面
        if (xyzId != null) {
            xyz = xyzService.findXyzDetail(xyzId);
        }
        return SUCCESS;
    }

    @Action("xyz")
    public String Xyz() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        sysDate = simple.format(new Date());
        return SUCCESS;
    }

    /**
     * 增加信用证（应付）
     */
    @Action("add_xyz")
    public void addXyz() {
        try {
//            xyz.setLxe(Double.parseDouble(xyz.getDqje().toString())*((getDateEdit(xyz.getDqrq(),xyz.getJkrq()))*(xyz.getYll()/100/30)));
            this.xyzService.saveXyz(xyz);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加应付票据成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("update_xyz")
    public void updateXyz() {
        try {
//            xyz.setLxe(Double.parseDouble(xyz.getDqje().toString())*((getDateEdit(xyz.getDqrq(),xyz.getJkrq()))*(xyz.getYll()/100/30)));
            this.xyzService.updateXyz(xyz);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改信用证成功！\", \"operationType\":\"update\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("delete_xyz")
    public void deleteXyz() {
        try {
            this.xyzService.deleteXyz(xyzId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除信用证成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //信用证未使用
    @Action("xyzwsyList")
    public void xyzwsyList() {
        this.renderJson(this.xyzService.findXyzWsy(xyzsflxType, page, rows));
    }

    /**
     * 进入信用证还款页面
     * @return
     */
    @Action("xyz_hk")
    public String xyzHk() {
        gsId =  this.xyzService.findXyzDetail(Long.parseLong(xyzIds.split(",")[0])).getNbgs().getId();
        gsMc = this.xyzService.findXyzDetail(Long.parseLong(xyzIds.split(",")[0])).getNbgs().getJc();
        return SUCCESS;
    }

    /**
     * 进入信用证还款页面
     * @return
     */
    @Action("xyz_bx")
    public String xyzBx() {
        gsId =  this.xyzService.findXyzDetail(Long.parseLong(xyzIds.split(",")[0])).getNbgs().getId();
        gsMc = this.xyzService.findXyzDetail(Long.parseLong(xyzIds.split(",")[0])).getNbgs().getJc();
        return SUCCESS;
    }

    /**
     * 信用证还款
     */
    @Action("hk_xyz")
    public void hkXyz() {
        try {
            this.xyzService.hkXyz(xyzIds,sqrId);
            renderText("{\"statusCode\":\"200\", \"message\":\"信用证还款成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 信用证变现
     */
    @Action("bx_xyz")
    public void bxXyz() {
        try {
            this.xyzService.bxXyz(xyzIds,sqrId,yhmcId);
            renderText("{\"statusCode\":\"200\", \"message\":\"信用证变现成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!this.xyzService.ajaxValidate(xyzId,validateVal,validateField))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }


    public Long getDateEdit(Date date1 ,Date date2){
        Long times = date1.getTime()-date2.getTime();
        return times/24/60/60/1000;
    }



    public Xyz getXyz() {
        return xyz;
    }

    public void setXyz(Xyz xyz) {
        this.xyz = xyz;
    }

    public Long getXyzId() {
        return xyzId;
    }

    public void setXyzId(Long xyzId) {
        this.xyzId = xyzId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public Integer getXyzsflxType() {
        return xyzsflxType;
    }

    public void setXyzsflxType(Integer xyzsflxType) {
        this.xyzsflxType = xyzsflxType;
    }

    public Integer getShztType() {
        return shztType;
    }

    public void setShztType(Integer shztType) {
        this.shztType = shztType;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public String getGsMc() {
        return gsMc;
    }

    public void setGsMc(String gsMc) {
        this.gsMc = gsMc;
    }

    public Long getKhhId() {
        return khhId;
    }

    public void setKhhId(Long khhId) {
        this.khhId = khhId;
    }

    public String getKhhMc() {
        return khhMc;
    }

    public void setKhhMc(String khhMc) {
        this.khhMc = khhMc;
    }

    public String getXyzIds() {
        return xyzIds;
    }

    public void setXyzIds(String xyzIds) {
        this.xyzIds = xyzIds;
    }

    public List<Xyz> getXyzList() {
        return xyzList;
    }

    public void setXyzList(List<Xyz> xyzList) {
        this.xyzList = xyzList;
    }

    public Long getSfkId() {
        return sfkId;
    }

    public void setSfkId(Long sfkId) {
        this.sfkId = sfkId;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Long getSqrId() {
        return sqrId;
    }

    public void setSqrId(Long sqrId) {
        this.sqrId = sqrId;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public Long getYhmcId() {
        return yhmcId;
    }

    public void setYhmcId(Long yhmcId) {
        this.yhmcId = yhmcId;
    }

    public String getXyzhm() {
        return xyzhm;
    }

    public void setXyzhm(String xyzhm) {
        this.xyzhm = xyzhm;
    }

    public String getSysDate() {
        return sysDate;
    }

    public void setSysDate(String sysDate) {
        this.sysDate = sysDate;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }



    @Autowired
    public void setXyzService(XyzService xyzService) {
        this.xyzService = xyzService;
    }
    @Autowired
    public void setKhhService(KhhService khhService) {
        this.khhService = khhService;
    }
    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
}
